package io.chenglicun.common.utils;





import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.util.Base64;

public class AESCBC {
    private static final String ALGORITHM = "AES/CBC/PKCS5Padding";

    public static String decryptData(String encryptedData, String key) throws Exception {
        byte[] encryptedBytes = Base64.getDecoder().decode(encryptedData);
        byte[] ivBytes = new byte[16];
        System.arraycopy(encryptedBytes, 0, ivBytes, 0, 16);
        byte[] dataBytes = new byte[encryptedBytes.length - 16];
        System.arraycopy(encryptedBytes, 16, dataBytes, 0, dataBytes.length);
        Cipher cipher = Cipher.getInstance(ALGORITHM);
        SecretKeySpec keySpec = new SecretKeySpec(key.getBytes(), "AES");
        IvParameterSpec ivSpec = new IvParameterSpec(ivBytes);
        cipher.init(Cipher.DECRYPT_MODE, keySpec, ivSpec);
        byte[] decryptedBytes = cipher.doFinal(dataBytes);
        return new String(decryptedBytes);
    }
}
